package bin_tree.heap.compare;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;

/**
 * @author yuisama
 * @date 2023/03/31 19:40
 **/

class StudentAgeSec implements Comparator<Student> {
    // > 0  => o1 > o2
    // =0 => o1 == o2
    // <0  => o1 < o2
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}

class StudentAgeDesc implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o2.age - o1.age;
    }
}



public class Student{
    String name;
    int age;
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public static void main(String[] args) {
        Student student1 = new Student("云超",21);
        Student student2 = new Student("铭弟",18);
        Student student3 = new Student("于新雨",18);
        Student student4 = new Student("鲍老师",30);
        Student student5 = new Student("刘鑫",40);
        Student[] students = {student1,student2,student3,student4,student5};
        // 现在需要取得年龄最小的2个学生
        Queue<Student> queue = new PriorityQueue<>(new StudentAgeSec());
        // 现在要取出年龄最小的两个学生，要保存在最大堆中，最大堆只保存2个元素
        for (Student student : students) {
            queue.offer(student);
            if (queue.size() > 2) {
                queue.poll();
            }
        }
        System.out.println(queue);
    }


}